<?php

namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\facade\Db;

class SitePush extends AdminBase
{

    public function index()
    {
        return view('');
    }

    public function project(){
        $parent_typelist = Db::name('data_type')->where('level',1)->select()->toArray();
        $weblist = Db::name('site_web')->select();
        return view('',['parent_typelist'=>$parent_typelist,'weblist'=>$weblist]);
    }

    public function article(){
        $parent_typelist = Db::name('data_type')->where('level',1)->select()->toArray();
        $weblist = Db::name('site_web')->select();
        return view('',['parent_typelist'=>$parent_typelist,'weblist'=>$weblist]);
    }

    //项目
    public function projectPush(){
        $param = $this->request->param();
        if(empty($param['webid'])){
            return json([
                'status' => 0,
                'msg' => '请选择站点'
            ]);
        }
        if(empty($param['num'])){
            return json([
                'status' => 0,
                'msg' => '推送数量为空'
            ]);
        }
        if($param['count'] >= $param['num']){
            return json([
                'status' => 0,
                'msg' => '执行完毕'
            ]);
        }
        $limit = 100;
        if($limit >= $param['num']){
            $limit = $param['num'];
        }
        $web = connect($param['webid']);
        $push = new \app\common\lib\Push($web);
        if(empty($push)){
            return json([
                'status' => 0,
                'msg' => '站点信息获取失败'
            ]);
        }
        $map[] = ['tort','=',0];
        $map[] = ['company_exist','=',1];
        if(!empty($param['parent_typeid'])){
            $map[] = ['parent_typeid','=',$param['parent_typeid']];
        }
        if(!empty($param['typeid'])){
            $map[] = ['typeid','=',$param['typeid']];
        }
        $makeChip = new \app\common\lib\MakeChip();
        $makeTable = new \app\common\lib\MakeTable();
        $list = Db::name('data_project')->field('id,typeid,parent_typeid,title,company,slogan,cost_min,cost_max,origin,shop_num,business,push_web')->whereRaw('!FIND_IN_SET('.$param['webid'].',push_web)')->where($map)->limit($limit)->select();
        foreach($list as $row){
            $row['dalei'] = get_typename($row['parent_typeid']);
            $row['xiaolei'] = get_typename($row['typeid']);
            $replace = [
                'wangzhan' => $web['title'],
                'xiangmu' => $row['title'],
                'dalei' => $row['dalei'],
                'xiaolei' => $row['xiaolei']
            ];
            $row['tiaojian'] = $makeChip->replaceContent($makeChip->getChip(1),$replace);
            $row['youshi'] = $makeChip->replaceContent($makeChip->getChip(2),$replace);
            $row['liucheng'] = $makeChip->replaceContent($makeChip->getChip(3),$replace);
            $row['zhichi'] = $makeChip->replaceContent($makeChip->getChip(4),$replace);
            $row['initData'] = $makeTable->initData($row['dalei']);
            $row['feiyong'] = $makeTable->getTable($row['initData'],$row['dalei'],'cost');
            $row['lirun'] = $makeTable->getTable($row['initData'],$row['dalei'],'profit');
            $r = $push->project($row);
            if($r){
                $push_web = explode(',',$row['push_web']);
                if(!in_array($param['webid'],$push_web)){
                    $push_web[] = $param['webid'];
                    $push_web = implode(',', $push_web);
                    Db::name('data_project')->where('id',$row['id'])->update(['push_web'=>$push_web]);
                }
            }
            $param['count']++;
        }

        return json([
            'status' => 1,
            'msg' => '正在执行，第'.$param['count'].'条',
            'data' => ['count'=>$param['count']]
        ]);
        // echo '<script>window.location.href="http://www.xnadmin.site/admin/SitePush/projectPush/?webid='.$param['webid'].'&num='.$param['num'].'&count='.$param['count'].'";</script>';
    }

    public function projectGetNum(){
        $param = $this->request->param();
        if(empty($param['webid'])){
            return json([
                'status' => 0,
                'msg' => '请选择站点'
            ]);
        }
        $map[] = ['tort','=',0];
        $map[] = ['company_exist','=',1];
        if(!empty($param['parent_typeid'])){
            $map[] = ['parent_typeid','=',$param['parent_typeid']];
        }
        if(!empty($param['typeid'])){
            $map[] = ['typeid','=',$param['typeid']];
        }
        $count = Db::name('data_project')->whereRaw('!FIND_IN_SET('.$param['webid'].',push_web)')->where($map)->count();
        return json([
            'status' => 1,
            'msg' => '执行成功',
            'data' => $count
        ]);
    }

    //文章
    public function articlePush(){
        $param = $this->request->param();
        if(empty($param['webid'])){
            return json([
                'status' => 0,
                'msg' => '请选择站点'
            ]);
        }
        if(empty($param['num'])){
            return json([
                'status' => 0,
                'msg' => '推送数量为空'
            ]);
        }
        if($param['count'] >= $param['num']){
            return json([
                'status' => 0,
                'msg' => '执行完毕'
            ]);
        }
        $limit = 100;
        if($limit >= $param['num']){
            $limit = $param['num'];
        }
        $web = connect($param['webid']);
        $push = new \app\common\lib\Push($web);
        if(empty($push)){
            return json([
                'status' => 0,
                'msg' => '站点信息获取失败'
            ]);
        }
        switch ($param['part']) {
            case '原创':
                $table = 'data_content_original';
            break;
            
            case '伪原创':
                $table = 'data_content_pseudo';
            break;

            case '组合':
                $table = 'data_content_compose';
            break;
        }
        $map = [];
        if(!empty($param['parent_typeid'])){
            $map[] = ['parent_typeid','=',$param['parent_typeid']];
        }
        if(!empty($param['typeid'])){
            $map[] = ['typeid','=',$param['typeid']];
        }
        $list = Db::name($table)->field('id,typeid,parent_typeid,project,title,keywords,description,content,attr,push_web')->whereRaw('!FIND_IN_SET('.$param['webid'].',push_web)')->where($map)->limit($limit)->select();
        foreach($list as $row){
            $row['dalei'] = get_typename($row['parent_typeid']);
            $row['xiaolei'] = get_typename($row['typeid']);
            $row['part'] = $param['part'];
            $row['catid'] = $param['catid'];
            $r = $push->article($row);
            if($r){
                $push_web = explode(',',$row['push_web']);
                if(!in_array($param['webid'],$push_web)){
                    $push_web[] = $param['webid'];
                    $push_web = implode(',', $push_web);
                    Db::name($table)->where('id',$row['id'])->update(['push_web'=>$push_web]);
                }
            }
            $param['count']++;
        }

        return json([
            'status' => 1,
            'msg' => '正在执行，第'.$param['count'].'条',
            'data' => ['count'=>$param['count']]
        ]);
        // echo '<script>window.location.href="http://www.xnadmin.site/admin/SitePush/articlePush/?part='.$param['part'].'&webid='.$param['webid'].'&catid='.$param['catid'].'&num='.$param['num'].'&count='.$param['count'].'";</script>';
    }

    public function articleGetNum(){
        $param = $this->request->param();
        if(empty($param['webid'])){
            return json([
                'status' => 0,
                'msg' => '请选择站点'
            ]);
        }
        switch ($param['part']) {
            case '原创':
                $table = 'data_content_original';
            break;
            
            case '伪原创':
                $table = 'data_content_pseudo';
            break;

            case '组合':
                $table = 'data_content_compose';
            break;
        }
        $map = [];
        if(!empty($param['parent_typeid'])){
            $map[] = ['parent_typeid','=',$param['parent_typeid']];
        }
        if(!empty($param['typeid'])){
            $map[] = ['typeid','=',$param['typeid']];
        }
        $count = Db::name($table)->whereRaw('!FIND_IN_SET('.$param['webid'].',push_web)')->where($map)->count();
        return json([
            'status' => 1,
            'msg' => '执行成功',
            'data' => $count
        ]);
    }

}
